package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * @param session  登录成功之后, 存储登录用户信息到session
     * @param employee 接收页面发送的登录信息
     * @return
     */
    @PostMapping("login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee) {
        //1. 对密码进行MD5加密
        String password = employee.getPassword(); // 明文密码
        password = DigestUtils.md5DigestAsHex(password.getBytes());  // 密文

        //2. 根据用户名和密码查询
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        lqw.eq(Employee::getPassword, password);  // 查询使用密文查询
        Employee emp = employeeService.getOne(lqw); // SELECT * FROM employee WHERE username = ? AND password = ?

        //3.判断用户是否存在
        if (emp == null) {
            return R.error("用户名或密码错误");
        }

        //4. 判断用户状态
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用!");
        }

        //5. 登录成功
        session.setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     *
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        //清理Session中保存的当前登录员工的id
        session.removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpSession session, @RequestBody Employee employee) {
        log.info("新增员工，员工信息：{}", employee.toString());

        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 分页查询
     *
     * @param page     当前页码
     * @param pageSize 每页显示多少条
     * @param name     查询条件
     * @return
     */
    @GetMapping("page")
    public R<Page> page(int page, int pageSize, String name) {
        //1. 分页构造器
        Page p = new Page(page, pageSize);

        //2. 查询条件构建器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);

        //3. 排序规则
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //4. 执行分页查询
        employeeService.page(p, queryWrapper);
        return R.success(p);
    }

    /**
     * 修改员工信息
     *
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpSession session) {

        //执行修改
        employeeService.updateById(employee);
        return R.success("修改成功");

    }


    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
}
